#!/bin/bash
##Docker安装脚本
install_docker__ce__install_docker(){
	Entering "Docker安装..."
	srv='docker-ce'
	if [[ "$Package_managements" == 'apt' ]];then
		srv='docker.io'
		echo '暂时使用io'
	elif [[ "$Package_managements" == 'yum' ]];then
		srv='docker-ce'
		service_chek "$srv"
		if [[ "$System_version" == '7' ]];then
			re=`yum list docker-ce --showduplicates|sort -r | awk '{print $2}' | awk 'NR>6{print}'|sed 's/base://'| awk NF |sed 's/3://'`
			for i in $re;do
				green_zt $i
			done
			green_zt "请选择一个docker版本并粘贴\n默认安装最新版"
			input "低版本很有可能安装失败,建议安装较新版本" "docker_ver"
			if [[ -z "$docker_ver" ]];then
				green_zt "已选择安装最新版本"
				dockers="$srv"
			else
				if [[ -z `echo $re | grep "$docker_ver"` ]];then
					red_bg "好像你输入的版本并不在上面的列表中"
					green_zt "已默认安装最新版"
				else
					green_zt "已选择安装${docker_ver}"
					srv="${srv}-${docker_ver} docker-ce-cli-${docker_ver}"
				fi
			fi
		fi
		Entering "Docker安装..."
		
	fi
	service_install "$srv" 
	chek_status "$?" "${srv}安装"
}
install_docker__ce__docker_config(){
	mkdir -p  /etc/docker/
	echo -e "{\n\t\"exec-opts\": [\"native.cgroupdriver=systemd\"],\n\t\"registry-mirrors\": [\"https://ung2thfc.mirror.aliyuncs.com\"]\n}" > /etc/docker/daemon.json
	service_start "docker"
	chek_status "$?" "Docker镜像加速地址配置"
	install_docker-ce_get_docker_version
	green_zt "[ Docker ]安装成功"
}
install_docker-ce_get_docker_version(){
    Dcoker_C_ver=`docker version | grep Version | awk '{print $2}' |sed -n 1p`
    Dcoker_S_ver=`docker version | grep Version | awk '{print $2}' |sed -n 2p`
	Docker_Cgroup=`docker info | grep 'Cgroup Driver' | sed 's/Cgroup Driver://'`
	red_z_green_z "Docker客户端版本:" "[ $Dcoker_C_ver ]"
	red_z_green_z "Docker服务端版本:" "[ $Dcoker_S_ver ]"
	red_z_green_z "当前Docker存储驱动:" "[ $Docker_Cgroup ]"
	red_z_green_z "当前镜像加速地址:" "[ 阿里云 ]"
}
install_docker_tools(){
	input "是否进入Docker工具箱 [ y/n ],默认[ n ]" docker_tools
	if [[ "$docker_tools" == 'y' ]];then
		Loading_function './tools/Docker_tools.sh'
		tools_menu-docker
	fi
}
install_docker_ce__docker_compose(){
	if [[ -e '/usr/sbin/docker-compose' ]];then
		green_zt '文件已存在'
	else
		curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/sbin/docker-compose
		chek_status "$?" "Docker-compose下载" 'q'
		chmod +x /usr/sbin/docker-compose
	fi
	docker-compose --version
	chek_status "$?" "docker-compose安装"
}
install_docker_harbor(){
	input '请选择安装模式\n[1]默认模式(自动选择IP地址、使用默认的密码)\n[2]自定义模式(自定义IP地址、密码等)' 'mode'
	input_check "$mode" "harbor安装模式" "1" "mode"
	#源码下载与检测
	if [[ -e '/opt/harbor-offline-installer-v2.1.2.tgz' ]];then
		if [[ -d '/opt/harbor' ]];then
			input "已存在解压目录: [ /opt/harbor/ ],是否删除 [ y/n ],默认[ y ]" r
			if [[ "$r" == 'n' ]];then
				green_zt "跳过删除: [ /opt/harbor ]"
			fi
		fi
	else
		if [[ -e '/usr/bin/wget' ]];then
			echo '跳过'
		else
			service_install 'wget'
		fi
		wget --no-check-certificate -O /opt/harbor-offline-installer-v2.1.2.tgz https://github.com/goharbor/harbor/releases/download/v2.1.2/harbor-offline-installer-v2.1.2.tgz
		chek_status "$?" 'Harbor源码下载' 'q'
	fi
	tar -zxf /opt/harbor-offline-installer-v2.1.2.tgz -C /opt/
	chek_status "$?" "Harbor源码解压"
	pushd /opt/harbor/
	cp /opt/harbor/harbor.yml.tmpl /opt/harbor/harbor.yml
	IP=`hostname -I | awk '{print $1}'`
	if [[ "$mode" == '2' ]];then
		input "请输入一个服务器IP地址,回车使用[ $IP ]" 'IP'

		input "请设置一个端口用于访问,默认[ 80 ]" 'port'
		input_check "$port" "harbor访问端口" "80" "port"

		input "请输入访问模式 [ http/https ],目前只能[HTTP]" 'visit'
		input_check " " "访问模式" 'http' 'visit'

		input "请输入管理员密码,默认 [ Harbor12345 ]" "admin_pass"
		input_check "$admin_pass" "Harbor管理员密码设置" "Harbor12345" "admin_pass"

		##修改端口
		sed -i "s#port: 80#port: $port#g" /opt/harbor/harbor.yml

		##修改管理员密码
		sed -i "s#harbor_admin_password.*#harbor_admin_password: ${admin_pass}#g" /opt/harbor/harbor.yml
		URL="http://${IP}:${port}"
	else
		URL="http://${IP}"
	fi
	#替换IP地址
	sed -i "s#hostname:.*#hostname: $IP#g" /opt/harbor/harbor.yml 
	#注释HTTPS配置
	sed -i "13,19s/^/#/" /opt/harbor/harbor.yml
	cd /opt/harbor/
	./install.sh
	chek_status "$?" "harbor服务部署"
	red_z_green_z "访问地址:" " [ $URL ]"
	red_z_green_z "登录用户:" " [ admin ]"
	red_z_green_z "登录密码:" " [ $admin_pass ]" 
}